//
//  HomeHeaderView.m
//  GC_OC
//
//  Created by 曾 on 2022/1/16.
//

#import "HomeHeaderView.h"
#import "DCCycleScrollView.h"
@interface HomeHeaderView()<DCCycleScrollViewDelegate>
@property (nonatomic, strong)UIView*            typeView;
@property (nonatomic, strong)DCCycleScrollView* cycleScrollView;
/**轮播图数据*/
@property (nonatomic, strong)NSArray* lbtArr;
/**内别数据*/
@property (nonatomic, strong)NSArray* mediaArr;

@property (nonatomic, strong)NSDictionary* dic;

@property (nonatomic, assign)BOOL showAll;

/** 更多按钮*/
@property (nonatomic, strong) UIButton *moreButton;
/** 收起按钮*/
@property (nonatomic, strong) UIButton *lessButton;

@property (nonatomic, assign) NSInteger line;

@property (nonatomic, strong) UIView *lineView;
@end
@implementation HomeHeaderView
-(instancetype)initWithFrame:(CGRect)frame andDic:(NSDictionary*)dic andShowAll:(BOOL)showAll
{
    self=[super initWithFrame:frame];
    if(self)
    {
        self.showAll=showAll;
        [_cycleScrollView removeFromSuperview];
        [_cycleScrollView invalidateTimer];
        _cycleScrollView=nil;
        
        [_typeView removeFromSuperview];
        _typeView=nil;
        self.dic=dic;
        [self addSubview:self.cycleScrollView];
        [self addSubview:self.typeView];
        if(self.lbtArr.count>0 || self.mediaArr.count>0)
        {
            self.lineView=[[UIView alloc]initWithFrame:CGRectMake(0,self.typeView.bottom,SCREEN_WIDTH,wid(20))];
            self.lineView.backgroundColor=RBG(247,247,247);
            [self addSubview:self.lineView];
            self.height = self.lineView.bottom;
        }else
        {
            self.height=self.typeView.bottom;
        }     
    }
    return  self;
}
-(void)stopAction
{
    [_cycleScrollView removeFromSuperview];
    [_cycleScrollView invalidateTimer];
    _cycleScrollView=nil;
    [_typeView removeFromSuperview];
    _typeView=nil;
}

- (DCCycleScrollView*)cycleScrollView
{
    if(!_cycleScrollView)
    {
        NSMutableArray* imageArr=[NSMutableArray new];
        NSMutableArray* titleArr=[NSMutableArray new];
        self.lbtArr=self.dic[@"data"][@"rotation_1"];
        for (int i=0;i<self.lbtArr.count;i++)
        {
            NSDictionary* dic=self.lbtArr[i];
            [imageArr addObject:self.lbtArr[i][@"cover_image"]];
            [titleArr addObject:self.lbtArr[i][@"rotation_name"]];
            NSLog(@"%@",dic[@"id"]);
        }
        if(self.lbtArr.count==1)
        {
            _cycleScrollView=[[DCCycleScrollView alloc]init];
            _cycleScrollView.frame=CGRectMake(wid(20),0,SCREEN_WIDTH-wid(40),wid(270));
            UIImageView* image=[[UIImageView alloc]initWithFrame:_cycleScrollView.bounds];
            [image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",imageArr[0]]]];
            image.contentMode=UIViewContentModeScaleAspectFill;
            image.layer.cornerRadius=wid(20);
            image.layer.masksToBounds=YES;
            [_cycleScrollView addSubview:image];
        }else
        {
            _cycleScrollView=[DCCycleScrollView cycleScrollViewWithFrame:CGRectMake(0,0,SCREEN_WIDTH,wid(270)) shouldInfiniteLoop:YES imageGroups:imageArr];
            _cycleScrollView.titleArr=titleArr;
            _cycleScrollView.autoScrollTimeInterval = 3;
            _cycleScrollView.autoScroll = YES;
            _cycleScrollView.infiniteLoop = YES;
            _cycleScrollView.isZoom = NO;
            _cycleScrollView.imgCornerRadius = 10;
            _cycleScrollView.itemWidth =SCREEN_WIDTH-wid(50);
            _cycleScrollView.itemSpace=wid(10);
            _cycleScrollView.delegate = self;
            if(self.lbtArr.count==0)
            {
                _cycleScrollView.frame=CGRectZero;
                [_cycleScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            }
        }
    }
    return _cycleScrollView;
}
-(UIView*)typeView
{
    if(!_typeView)
    {
        self.mediaArr=self.dic[@"data"][@"media_class"];
        CGFloat jx=wid(20);
        CGFloat jy=wid(15);
        CGFloat width=(SCREEN_WIDTH-jx*5)/4.0;
        //计算行数,如果行数小于等于2则直接显示行数,若大于2则显示更多.
        self.line = (self.mediaArr.count - 1) / 4 + 1;
        CGFloat viewH = self.line >= 2 ? jy*3+wid(55)*2 : jy*2+wid(55);
        _typeView=[[UIView alloc]initWithFrame:CGRectMake(0,self.cycleScrollView.height,SCREEN_WIDTH,viewH)];
        _typeView.clipsToBounds = YES;
        
        //加载子控件
        for(int i=0;i<self.mediaArr.count;i++)
        {
            NSInteger row = i / 4;
            NSInteger line = i % 4;
            UIButton* btn=[[UIButton alloc]initWithFrame:CGRectMake(jx + (width + jx) * line,jy + (wid(55) + jy) * row,width,wid(55))];
            btn.backgroundColor=RBG(240,240,240);
            btn.layer.masksToBounds=YES;
            btn.layer.cornerRadius=wid(10);
            [btn setTitle:self.mediaArr[i][@"name"] forState:UIControlStateNormal];
            btn.titleLabel.font=[UIFont systemFontOfSize:FontSize(12)];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [_typeView addSubview:btn];
            btn.tag=200+i;
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            
            if (self.line > 2 && i == 7) {
                self.moreButton = btn;
                [btn setTitle:@"更多" forState:UIControlStateNormal];
            }
        }
        if (self.line > 2) {  //添加关闭按钮
            NSInteger row = self.mediaArr.count / 4;
            NSInteger line = self.mediaArr.count % 4;
            UIButton* btn=[[UIButton alloc]initWithFrame:CGRectMake(jx + (width + jx) * line,jy + (wid(55) + jy) * row,width,wid(55))];
            btn.backgroundColor=RBG(240,240,240);
            btn.layer.masksToBounds=YES;
            btn.layer.cornerRadius=wid(10);
            [btn setTitle:@"收起" forState:UIControlStateNormal];
            btn.titleLabel.font=[UIFont systemFontOfSize:FontSize(12)];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [_typeView addSubview:btn];
            btn.tag=200+self.mediaArr.count;
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            self.lessButton = btn;
        }
        if(self.mediaArr.count==0)
        {
            _typeView.frame=CGRectZero;
            [_typeView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        }
    }
    return _typeView;
}

-(BOOL)haveDate
{
    if(self.lbtArr.count==0 && self.mediaArr==0)
    {
        return false;
    }
    return YES;
}

-(void)btnAction:(UIButton*)sender
{
    CGFloat jy=wid(15);
    if ([sender.titleLabel.text isEqualToString:@"更多"]) {
        [self.moreButton setTitle:self.mediaArr[7][@"name"] forState:UIControlStateNormal];
        self.typeView.height=self.line * (wid(55) + jy) + jy;
        self.lineView.frame = CGRectMake(0,self.typeView.bottom,SCREEN_WIDTH,wid(20));
        self.height = self.lineView.bottom;
        if ([_myDelegate respondsToSelector:@selector(heightChange)]) {
            [_myDelegate heightChange];
        }
        return;
    } else if ([sender.titleLabel.text isEqualToString:@"收起"]) {
        [self.moreButton setTitle:@"更多" forState:UIControlStateNormal];
        self.typeView.height=2 * (wid(55) + jy) + jy;
        self.lineView.frame = CGRectMake(0,self.typeView.bottom,SCREEN_WIDTH,wid(20));
        self.height = self.lineView.bottom;
        if ([_myDelegate respondsToSelector:@selector(heightChange)]) {
            [_myDelegate heightChange];
        }
        return;
    }
    [self.myDelegate HomeHeaderTitleClick:sender.tag-200];
}

@end
